package com.medic.client.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.medic.service.impl.WXServiceImpl;
import com.medic.util.CacheUtil;
import com.medic.util.JsonUtil;
import com.medic.util.StringUtil;

/**
 * 微信端操作：如下载微信服务器上面的图片（调用微信拍照功能图片需要上传至微信服务器后才能下载）
 * 
 * @author Mr.zhang
 *
 */
@Controller
@RequestMapping("client/wx")
public class WXController {

	@Autowired
	private HttpServletRequest request;
	
	@Resource(name="wxService")
	private WXServiceImpl wxService;
	
	private final String mediaKey = "mediaId";
	private final String resultKey = "result";

	@RequestMapping("/downimg")
	@ResponseBody
	public String downImgFromWxServer(String data) {
		
		String resJson = "";
		try {
			String mediaId = JsonUtil.getValue(data, mediaKey);
			String path = request.getSession().getServletContext()
					.getRealPath(CacheUtil.getConfig().getProperty(StringUtil.FILE_UPLOAD));
			String fileName = StringUtil.getFileName("");
			fileName = wxService.downImg(mediaId, path, fileName);
			if(fileName != null && !fileName.equals("")){
				resJson = "{'"+resultKey+"':'"+fileName+"'}";
			}else{
				resJson = "{'"+resultKey+"':'fail'}";
			}
			
		} catch (Exception e) {
			resJson = "{'"+resultKey+"':'error'}";
		}

		return resJson;
	}

}
